/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.taglib.head.title;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.exception.technical.TechnicalException;

/**
 * taglib allowing to define the title of the current jsp.
 */
@SuppressWarnings("serial")
public class SpringTitle
    extends BodyTagSupport
{

  /**
   * the title text.
   */
  private String text;

  /**
   * the priority.
   */
  private int priority = 0;

  /**
   * the titlesContainer.
   */
  private TitleContainer titlesContainer;



  /**
   * setter for the text property.
   * 
   * @param text
   *          the text to set
   */
  public void setText(String text)
  {
    this.text = text;
  }



  /**
   * getter for the text property.
   * 
   * @return the text
   */
  public String getText()
  {
    return text;
  }



  /**
   * Processing of the end tag.
   * 
   * @return EVAL_PAGE
   * @throws JspException
   *           If two titles have the same priority
   */
  @Override
  public int doEndTag()
      throws JspException
  {
    try {
      if (!StringUtils.hasText(text)) {
        text = getBodyContent().getString();
      }
      getTitlesContainer().addTitle(text, priority);
    }
    catch (TechnicalException e) {
      throw new JspException(e.getMessage());
    }

    return EVAL_PAGE;

  }



  /**
   * Release state.
   */
  @Override
  public void release()
  {
    super.release();
    text = null;
    priority = 0;
  }



  /**
   * The setter method for the priroty property.
   * 
   * @param priority
   *          the priority to set
   */
  public void setPriority(int priority)
  {
    this.priority = priority;

  }



  /**
   * getter for the titlesContainer property.
   * 
   * @return the titlesContainer
   */
  public TitleContainer getTitlesContainer()
  {
    return titlesContainer;
  }



  /**
   * The setter method for the titlesContainer property.
   * 
   * @param titlesContainer
   *          the titlesContainer to set
   */
  public void setTitlesContainer(TitleContainer titlesContainer)
  {
    this.titlesContainer = titlesContainer;
  }



  /**
   * getter for the priority property.
   * 
   * @return the priority
   */
  public int getPriority()
  {
    return priority;
  }

}
